<?php

/*
 *    Document   : images
 *   Created on : Jun 11, 2011, 3:55:17 PM
 *    Author     : ClarkT AKA FalleN
 *    Company    : Amodity
 *    Web        : http://www.amodity.com
 *    Copyright  : 2011
 */

/**

 * @property CI_Loader $load

 * @property CI_Form_validation $form_validation

 * @property CI_Input $input

 * @property CI_Email $email

 * @property CI_DB_active_record $db

 * @property CI_DB_forge $dbforge

 *  @property CI_Session $session

 * @property CI_FTP $ftp

 */
/*
 *  Admin Images Model
 */

class images_model extends CI_Model {

    public function __construct() {
        parent::__construct();
        $this->load->config('auth', TRUE);
        $this->tables = $this->config->item('tables', 'auth');
    }

    function get_images() {

        //generate table
        $this->load->library('table');
        $this->db->order_by('id', 'DESC');
        $query = $this->db->get('images');

        $this->table->set_heading('ID', lang('admin_preview'), lang('g_date'), lang('g_size'), lang('g_ipaddress'), lang('admin_author'), lang('admin_actions'));
        foreach ($query->result() as $row) {
            $links = anchor('admin/images/delete_image/' . $row->id, '<span class="icon_single cancel"></span>', array('class' => 'button white'));
            $this->config->load('upload');
            $thumb = array('data' => '<a class="lightbox" rel="lightbox-gallery" href="' . base_url() . substr($this->config->item('upload_path'), 2) . $row->name . '" title="' . $row->name . '"><img src="' . base_url() . substr($this->config->item('upload_path'), 2) . $row->name . '" alt="image-gallery"><b>' . $row->name . '</b></a>', 'class' => 'gallery-td');

            $this->table->add_row(
                    $row->id, $thumb, $row->date, $row->height . ' x ' . $row->width, $row->ip_address, $row->author, $links
            );

            $this->table->set_template(array('table_open' => '<table class="display imgmanage">', 'row_alt_start' => '<tr class="odd">', 'row_alt_end' => '</tr>', 'table_close' => '</table>'));
        }
    }

    function delete_image($image_id) {
        //get image name by id
        $this->db->where('id', $image_id);
        $data = $this->db->get('images');
        if ($data->num_rows() > 0) {

            $imagedata = $data->row();
            $imagename = $imagedata->name;

            //remove image and db record
            $this->db->where('name', $imagename);
            $this->db->delete('images');
            $this->config->load('upload');

            $path = substr($this->config->item('upload_path'), 0) . $imagename;
            $remove = unlink($path);

            if ($remove) {
                $this->message->set('success', lang('admin_file_remove_success'));
            } else {
                $this->message->set('error', lang('admin_file_remove_fail'));
            }
        } else {
            return $this->message->set('error', 'Image does not exsist');
        }
    }

    function _getuserid($username) {
        $this->db->where('username', $username);
        $this->db->select('id');
        return $query = $this->db->get('users');
    }

}

?>